'use strict';

const EsEnum = require('../commons/EsEnum');

// 返回码列表
const _RespCodeEnum = new EsEnum([
    {
        name: 'ErrCode_0000',
        code: '0000',
        msg: '操作成功'
    },
    {
        name: 'ErrCode_0001',
        code: '0001',
        msg: '参数错误'
    },
    {
        name: 'ErrCode_0003',
        code: '0003',
        msg: '用户Token失效或不正确。'
    },
    {
        name: 'ErrCode_0004',
        code: '0004',
        msg: '未查到符合条件的数据。'
    },
    {
        name: 'ErrCode_0005',
        code: '0005',
        msg: '用户无此操作权限。'
    },
    {
        name: 'ErrCode_0006',
        code: '0006',
        msg: '要添加的数据重复。'
    },
    {
        name: 'ErrCode_0007',
        code: '0007',
        msg: '此电脑无此操作权限。'
    },
    {
        name: 'ErrCode_0008',
        code: '0008',
        msg: '用户无此操作权限。'
    },
    {
        name: 'ErrCode_0009',
        code: '0009',
        msg: '该模板已经被使用，不可以进行编辑。'
    },
    {
        name: 'ErrCode_0010',
        code: '0010',
        msg: '该标签已经在使用中，请检查是否输入正确。'
    },
    {
        name: 'ErrCode_0011',
        code: '0011',
        msg: '该标的物编号已经在使用中，请检查是否输入正确。'
    },
    {
        name: 'ErrCode_0012',
        code: '0012',
        msg: '该合同状态不合法，请再次确认。'
    },
    {
        name: 'ErrCode_0013',
        code: '0013',
        msg: '该标的物编号已经被布控，请先撤销布控再修改。'
    },
    {
        name: 'ErrCode_0014',
        code: '0014',
        msg: '该合同模板已经作废，请选择其他模板。'
    },
    {
        name: 'ErrCode_0015',
        code: '0015',
        msg: '该合同模板正在被审核中，请选择其他合同进行审核。'
    },
    {
        name: 'ErrCode_0016',
        code: '0016',
        msg: '队列中暂无待审核合同。'
    },
    {
        name: 'ErrCode_0017',
        code: '0017',
        msg: '该保险项不存在。'
    },
    {
        name: 'ErrCode_0018',
        code: '0018',
        msg: '保险项id与合同id匹配。'
    },
    {
        name: 'ErrCode_0019',
        code: '0019',
        msg: '该外勤人员已经在出警中了。'
    },
    {
        name: 'ErrCode_0020',
        code: '0020',
        msg: '该案件已经结案，不可以派遣外勤人员出警。'
    },
    {
        name: 'ErrCode_0021',
        code: '0021',
        msg: '你的出警已结束。'
    },
    {
        name: 'ErrCode_0022',
        code: '0022',
        msg: '该出警记录不属于你。'
    },
    {
        name: 'ErrCode_0023',
        code: '0023',
        msg: '该案件已经结案。'
    },
    {
        name: 'ErrCode_0024',
        code: '0024',
        msg: '该案件还有出警中的人员，请结束所有出警再结案。'
    },
    {
        name: 'ErrCode_0025',
        code: '0025',
        msg: '该已经被使用无法修改。'
    },
    {
        name: 'ErrCode_0026',
        code: '0026',
        msg: '队列中暂无需要拨打的任务。'
    },
    {
        name: 'ErrCode_1001',
        code: '1001',
        msg: '用户已存在。'
    },
    {
        name: 'ErrCode_1002',
        code: '1002',
        msg: '用户不存在。'
    },
    {
        name: 'ErrCode_1003',
        code: '1003',
        msg: '密码错误'
    },

    {
        name: 'ErrCode_9999',
        code: '9999',
        msg: '系统错误，请联系管理员。'
    }
]);

/**
 * 挂在 扩展方法到 response对象上
 */
module.exports = {
    RespCodeEnum: _RespCodeEnum,
    // 成功返回
    success(data) {
        this.status = 200;
        this.body = {
            respCode: this.RespCodeEnum.ErrCode_0000.code,
            respMsg: this.RespCodeEnum.ErrCode_0000.msg,
            respData: data
        };
        // this.app.logger.debug('成功返回数据：', this.body);
    },
    // 通用失败返回
    fail(msg, data) {
        this.status = 200;
        this.body = {
            respCode: this.RespCodeEnum.ErrCode_9999.code,
            respMsg: msg || this.RespCodeEnum.ErrCode_9999.msg,
            respData: data
        };
    },
    // 失败返回
    failByCode(code, status, msg, data) {
        const erroCodeName = 'ErrCode_' + code;
        switch (code) {
            case _RespCodeEnum.valueOf(erroCodeName).code:
                this.status = status || 200;
                this.body = {
                    respCode: this.RespCodeEnum.valueOf(erroCodeName).code,
                    respMsg: msg || this.RespCodeEnum.valueOf(erroCodeName).msg,
                    respData: data
                };
                break;

            default:
                this.status = status || 200;
                this.body = {
                    respCode: this.RespCodeEnum.ErrCode_9999.code,
                    respMsg: msg || this.RespCodeEnum.ErrCode_9999.msg,
                    respData: data
                };
                break;
        }
    },
    // token错误返回
    tokenError(status) {
        switch (status) {
            case 401:
                this.status = 401;
                this.body = {
                    respCode: this.RespCodeEnum.ErrCode_0003.code,
                    respMsg: this.RespCodeEnum.ErrCode_0003.msg
                };
                break;

            case 403:
                this.status = 403;
                this.body = {
                    respCode: this.RespCodeEnum.ErrCode_0003.code,
                    respMsg: this.RespCodeEnum.ErrCode_0003.msg
                };
                break;

            default:
                this.status = 401;
                this.body = {
                    respCode: this.RespCodeEnum.ErrCode_0003.code,
                    respMsg: this.RespCodeEnum.ErrCode_0003.msg
                };
                break;
        }
    }
};
